package com.drocode.swithcer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImageActivity extends Activity {
	public HashMap<String,Bitmap> imagesCache=new HashMap<String, Bitmap>(); //图片缓存  
	public List<String> urls ;
	public GuideGallery images_ga;
	private int positon = 0;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
  
  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.url_connection_image);  
    }  
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();  
    	init();
    }
    private void init(){  
        Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.icon);  
        imagesCache.put("background_non_load",image);       //设置缓存中默认的图片  
  
        images_ga = (GuideGallery) findViewById(R.id.image_wall_gallery);  
        urls = new ArrayList<String>(); //图片地址List  
        urls.add("http://fujian.xabbs.com/forum/201109/02/160646nn9hjjiimixvkxhe.jpg");  
        urls.add("http://img1.cache.netease.com/catchpic/A/A9/A9D98040B397C366AE93E67871346561.jpg");  
        urls.add("http://new.aliyiyao.com/UpFiles/Image/2011/01/13/nc_129393721364387442.jpg");  
        urls.add("http://pic.viewpics.cn/2011/07/03/naichaMMzhangzetianzuixinzhaopian/18.jpg");  
        urls.add("http://i1.sinaimg.cn/ent/m/c/2010-01-18/U1819P28T3D2847679F326DT20100118115712.jpg");  
        urls.add("http://comic.sinaimg.cn/2011/0824/U5237P1157DT20110824161051.jpg");  
        ImageAdapter imageAdapter = new ImageAdapter(urls,this);  
        images_ga.setAdapter(imageAdapter);  
        LinearLayout pointLinear = (LinearLayout) findViewById(R.id.gallery_point_linear);
        pointLinear.setBackgroundColor(Color.argb(200, 135, 135, 152));
        for (int i = 0; i < urls.size(); i++) {
        	ImageView pointView = new ImageView(this);
        	if(i==0){
        		pointView.setBackgroundResource(R.drawable.feature_point_cur);
        	}else
        		pointView.setBackgroundResource(R.drawable.feature_point);
        	pointLinear.addView(pointView);
		}
        
        autoGallery.schedule(new TimerTask() {
            int gallerypisition = 0;
            @Override
            public void run() {
                gallerypisition = images_ga.getSelectedItemPosition() + 1;
                Message msg = new Message();
                Bundle date = new Bundle();// 存放数据
                date.putInt("pos", gallerypisition);
                msg.setData(date);
                msg.what = 1;//消息标识
                autoGalleryHandler.sendMessage(msg);
            }
            }, 5000, 5000);
    }  
    
    public void changePointView(int cur){
    	LinearLayout pointLinear = (LinearLayout) findViewById(R.id.gallery_point_linear);
    	View view = pointLinear.getChildAt(positon);
    	View curView = pointLinear.getChildAt(cur);
    	if(view!=null&& curView!=null){
    		ImageView pointView = (ImageView)view;
    		ImageView curPointView = (ImageView)curView;
    		pointView.setBackgroundResource(R.drawable.feature_point);
    		curPointView.setBackgroundResource(R.drawable.feature_point_cur);
    		positon = cur;
    	}
    }
    
    final Handler autoGalleryHandler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            switch (message.what) {
                case 1:
                	images_ga.setSelection(message.getData().getInt("pos"));
                    break;
            }
            }
    }; 
    
    Timer autoGallery = new Timer();
}